package webjoch.misc;

import java.util.ArrayList;

import webjoch.Main;

import com.rsbuddy.script.ActiveScript;
import com.rsbuddy.script.methods.Calculations;
import com.rsbuddy.script.methods.Objects;
import com.rsbuddy.script.methods.Players;
import com.rsbuddy.script.methods.Walking;
import com.rsbuddy.script.wrappers.GameObject;
import com.rsbuddy.script.wrappers.Tile;

/**
 * A second loop beside the main loop
 * @author Jochem
 *
 */
public class Checker extends ActiveScript{
	@Override
	public int loop(){
		obstructingKeyDoors();
		return 0;
	}
	
	/**
	 * Checks for obstructing key doors and moves to the center of the room
	 * if there is one
	 */
	public void obstructingKeyDoors(){
		Tile flag = Walking.getDestination();
		GameObject nearestKeyDoor = Objects.getNearest(getAllKeyDoorsIds());
		if (nearestKeyDoor != null && Calculations.distanceTo(flag) > 5 && Calculations.distanceTo(nearestKeyDoor) < 3){
			sleep(1000);
			if (!Players.getLocal().isMoving()){
				log("Obstructing key door: " + nearestKeyDoor.getId());
				Main.getDungeon().getCurrentRoom().getLocation().clickOnMap();
				sleep(1000);
				flag.clickOnMap();
				sleep(5000);
			}
		}
	}
	
	/**
	 * Get all key door ids
	 * @return a single array of all key door ids
	 */
	public int[] getAllKeyDoorsIds(){
		ArrayList<Integer> doorIds = new ArrayList<Integer>();
		for (int[] keyDoors : Constants.KEY_DOORS){
			for (int keyDoor : keyDoors){
				doorIds.add(keyDoor);
			}
		}
		int[] intArray = new int[doorIds.size()];
		for (int i = 0; i < doorIds.size(); i++) {
			intArray[i] = doorIds.get(i);
		}
		return intArray;
	}
}
